package com.flyou.ccnucenter.ui.activity.feedBack;


import android.app.ProgressDialog;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.e1858.myalertview.AlertView;
import com.e1858.myalertview.OnItemClickListener;
import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.domain.FeedBack;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.utils.BmobErrorCodeUtils;
import com.flyou.ccnucenter.utils.SPUtils;

import cn.bmob.v3.listener.SaveListener;

public class FeedBackActivity extends BaseActivity1 implements View.OnClickListener, TextWatcher, OnItemClickListener {

    private int selectionStart;
    private int selectionEnd;
    private int totalNum = 300;
    private CharSequence tempCount;
    private String feedbackTypes[] = {"功能意见", "页面意见", "您的新需求", "操作意见", "其他"};
    private String currentType = "功能意见";
    private RelativeLayout titlebar_iv_back;
    private RelativeLayout titlebar_iv_search;
    private TextView titlebar_tv_title;
    private TextView titlebar_tv_text_right;

    private LinearLayout ll_feedback_type;
    private TextView tv_feedback_type_text;
    private EditText et_feedback_text_content;
    private TextView tv_feedback_text_length;
    private EditText et_feedback_contact;


    private AlertView mAlertView;


    @Override
    protected void initView() {
        setContentView(R.layout.activity_feed_back);

//        titlebar
        titlebar_iv_back = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        titlebar_iv_search = (RelativeLayout) findViewById(R.id.titlebar_rl_Right);
        titlebar_tv_text_right = (TextView) findViewById(R.id.titlebar_tv_text_right);
        titlebar_tv_title = (TextView) findViewById(R.id.titlebar_tv_title);

        titlebar_iv_search.setVisibility(View.GONE);
        titlebar_tv_text_right.setVisibility(View.VISIBLE);
        titlebar_tv_text_right.setText("提交");
        titlebar_tv_title.setText("意见反馈");
//        意见反馈类型
        ll_feedback_type = (LinearLayout) findViewById(R.id.ll_feedback_type);
        tv_feedback_type_text = (TextView) findViewById(R.id.tv_feedback_type_text);
//        意见反馈内容
        et_feedback_text_content = (EditText) findViewById(R.id.et_feedback_text_content);
        tv_feedback_text_length = (TextView) findViewById(R.id.tv_feedback_text_length);
        et_feedback_contact = (EditText) findViewById(R.id.et_feedback_contact);


    }

    @Override
    protected void initDate() {
        titlebar_tv_title.setText("用户反馈");
    }

    @Override
    protected void setListener() {
        titlebar_iv_back.setOnClickListener(this);
        titlebar_tv_text_right.setOnClickListener(this);
        ll_feedback_type.setOnClickListener(this);
        et_feedback_text_content.addTextChangedListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.titlebar_rl_back:
                finish();
                break;

            case R.id.titlebar_tv_text_right:
                String conetnt = et_feedback_text_content.getText().toString().trim();
                if (TextUtils.isEmpty(conetnt)) {
                    Toast.makeText(this, "您的反馈信息不能为空哦！", Toast.LENGTH_SHORT).show();
                    return;
                }
                String contact = et_feedback_contact.getText().toString().trim();
                String userName = (String) SPUtils.get(FeedBackActivity.this, "userName", "");
                String phone = (String) SPUtils.get(FeedBackActivity.this, "phone", "");
                String email = (String) SPUtils.get(FeedBackActivity.this, "email", "");
                String objectId = (String) SPUtils.get(FeedBackActivity.this, "objectId", "");

                FeedBack feedBack = new FeedBack(currentType, contact, conetnt, objectId, userName, phone, email);
                feedBack.save(FeedBackActivity.this, new SaveListener() {

                    @Override
                    public void onFinish() {
                        mProgressDialog.dismiss();

                        super.onFinish();
                    }

                    @Override
                    public void onStart() {
                        mProgressDialog = ProgressDialog.show(FeedBackActivity.this, null, "正在提交", false, false);
                        super.onStart();
                    }

                    @Override
                    public void onSuccess() {
                        Toast.makeText(FeedBackActivity.this, "您的反馈已经提交，我将近期与您取得联系，感谢您的支持！", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        Toast.makeText(FeedBackActivity.this, BmobErrorCodeUtils.getMessage(i, s), Toast.LENGTH_SHORT).show();

                    }
                });
                break;
            case R.id.ll_feedback_type:
                mAlertView = new AlertView("反馈类型", null, null, null,
                        feedbackTypes,
                        this, AlertView.Style.ActionSheet, this);
                mAlertView.show();
                break;

        }
    }

    @Override
    public void onItemClick(Object o, int position) {

        currentType = feedbackTypes[position];
        tv_feedback_type_text.setText(currentType);


    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        tempCount = s;
    }

    @Override
    public void afterTextChanged(Editable s) {
        int number = totalNum - s.length();
        tv_feedback_text_length.setText(totalNum - number + "/" + totalNum);
        selectionStart = et_feedback_text_content.getSelectionStart();
        selectionEnd = et_feedback_text_content.getSelectionEnd();
        if (tempCount.length() > totalNum) {
            s.delete(selectionStart - 1, selectionEnd);
            int tempSelection = selectionEnd;
            et_feedback_text_content.setText(s);
            et_feedback_text_content.setSelection(tempSelection);//设置光标在最后
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (mAlertView != null && mAlertView.isShowing()) {
                mAlertView.dismiss();
                return false;
            }
        }

        return super.onKeyDown(keyCode, event);

    }
}
